package xin.marcher.module.replenished.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Repository;
import xin.marcher.framework.common.util.data.DataCuttingUtil;
import xin.marcher.module.common.constants.replenished.RecoverableConstants;
import xin.marcher.module.common.enums.DelFlagEnum;
import xin.marcher.module.common.enums.seller.SellerTypeEnum;
import xin.marcher.module.common.enums.seller.SkuSellerRelationTypeEnum;
import xin.marcher.module.replenished.domain.entity.SkuSellerRelationDO;
import xin.marcher.module.replenished.domain.entity.SkuStockSellerRelationDO;
import xin.marcher.module.replenished.domain.entity.StackSkuRelationDO;
import xin.marcher.module.replenished.mapper.SkuSellerRelationMapper;
import xin.marcher.module.replenished.mapper.SkuStockSellerRelationMapper;
import xin.marcher.module.replenished.mapper.StackSkuRelationMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 商品 关系表相关资源类
 *
 * 
 */
@Repository
public class ProductRelationRepository {

    @Resource
    private SkuSellerRelationMapper skuSellerRelationMapper;

    @Resource
    private StackSkuRelationMapper stackSkuRelationMapper;

    @Resource
    private SkuStockSellerRelationMapper skuStockSellerRelationMapper;

    /**
     * 分页查询 卖家组ID 可售商品信息
     *
     * @param sellerGroupIds
     * @return
     */
    public List<SkuSellerRelationDO> pageQueryAvailableProduct(List<Long> sellerGroupIds) {
        // 默认集合初始大小为50000，避免扩容次数频繁，一般商品的数量最多也就是几万-几十万
        List<SkuSellerRelationDO> sellerRelationAllList = new ArrayList<>(RecoverableConstants.SKU_INIT_NUM);
        // 一次最大查询10 卖家组ID，多个分页查询,这里做数据切割
        List<List<Long>> splitList = DataCuttingUtil.dataCuttingString(sellerGroupIds, RecoverableConstants.SELLER_ID_LIMIT_NUM);
        for (List<Long> skuIds : splitList) {
            List<SkuSellerRelationDO> productDetailDOList = queryAvailableProduct(skuIds);
            if (!CollectionUtils.isEmpty(productDetailDOList)) {
                sellerRelationAllList.addAll(productDetailDOList);
            }
        }
        return sellerRelationAllList;
    }

    /**
     * 根据卖家组ID 批量查询 可售商品信息
     *
     * @param sellerGroupIds
     */
    public List<SkuSellerRelationDO> queryAvailableProduct(List<Long> sellerGroupIds) {
        // 获取卖家组对应的可售商品
        LambdaQueryWrapper<SkuSellerRelationDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(SkuSellerRelationDO::getSellerGroupId, sellerGroupIds)
                .eq(SkuSellerRelationDO::getRelationType, SkuSellerRelationTypeEnum.RELATION_TYPE_YES.getCode())
                .eq(SkuSellerRelationDO::getDelFlag, DelFlagEnum.EFFECTIVE.getCode());
        return skuSellerRelationMapper.selectList(queryWrapper);
    }

    /**
     * 分页批量查询 组套商品信息
     *
     * @param skuIds
     * @return
     */
    public List<StackSkuRelationDO> pageQueryStackSkuListByIds(Set<String> skuIds) {
        // 默认集合初始大小为50000，避免扩容次数频繁，一般商品的数量最多也就是几万-几十万
        List<StackSkuRelationDO> skuRelationDOAllList = new ArrayList<>(skuIds.size());
        // 一次最大查询1000 skuId，多个分页查询,这里做数据切割
        List<Set<String>> splitList = DataCuttingUtil.dataCuttingString(skuIds, RecoverableConstants.SKU_LIMIT_NUM);
        for (Set<String> skuList : splitList) {
            List<StackSkuRelationDO> skuRelationDOList = queryStackSkuListByIds(skuList);
            if (!CollectionUtils.isEmpty(skuRelationDOList)) {
                skuRelationDOAllList.addAll(skuRelationDOList);
            }
        }
        return skuRelationDOAllList;
    }

    /**
     * 根据skuId集合批量查询组套商品信息
     *
     * @param skuIds
     * @return
     */
    public List<StackSkuRelationDO> queryStackSkuListByIds(Set<String> skuIds) {
        LambdaQueryWrapper<StackSkuRelationDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(StackSkuRelationDO::getSkuId, skuIds)
                .eq(StackSkuRelationDO::getSellerType, SellerTypeEnum.SELF.getCode());
        return stackSkuRelationMapper.selectList(queryWrapper);
    }

    /**
     * 查询自营商品库存大于0的卖家库存数据
     *
     * @param skuIds
     * @return
     */
    public List<SkuStockSellerRelationDO> querySellerInStockProductList(List<String> skuIds) {
        LambdaQueryWrapper<SkuStockSellerRelationDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(SkuStockSellerRelationDO::getSkuId, skuIds)
                .eq(SkuStockSellerRelationDO::getSellerType, SellerTypeEnum.SELF.getCode())
                .gt(SkuStockSellerRelationDO::getStockNum, 0);
        return skuStockSellerRelationMapper.selectList(queryWrapper);
    }

}
